var t = require("../../api.js"),
  e = getApp(),
  a = 0,
  i = 0,
  s = require("../../utils/utils.js"),
  n = !1;

Page({
  data: {
    total_price: 0,
    address: null,
    express_price: 0,
    express_price_1: 0,
    integral_radio: 1,
    new_total_price: 0,
    show_card: !1,
    payment: -1,
    show_payment: !1,
    show_more: !1,
    index: -1,
    mch_offline: !0,
    is_peisong: 0,
    form_id: [],
    editTrue: false,
    distance:-1,
    peisong_time:[
      { index: 0, time: '任何时间' },
      { index: 1, time: '8:00~9:00' },
      { index: 2, time: '9:00~10:00' },
      { index: 3, time: '10:00~11:00' },
      { index: 4, time: '11:00~12:00' },
      { index: 5, time: '12:00~13:00' },
      { index: 6, time: '13:00~14:00' },
      { index: 7, time: '14:00~15:00' },
      { index: 8, time: '15:00~16:00' },
      { index: 9, time: '16:00~17:00' },
      { index: 10, time: '17:00~18:00' },
      { index: 11, time: '18:00~19:00' },
      { index: 12, time: '19:00~20:00' },
    ],
    show_time:0,
    change_peisong_time:'任何时间',
    pick_coupon_index:-1
  },
  onLoad: function(t) {
    e.pageOnLoad(this, t);
    var a = this,
      i = s.formatData(new Date());
    wx.removeStorageSync("input_data"), a.setData({
      options: t,
      store: wx.getStorageSync("store"),
      time: i
    }), n = !1;
    this.themeColor();
  },
  themeColor: function () {
    var cl = wx.getStorageSync('_navigation_bar_color').themeColor;
    var cl_b = !cl || cl == '#ffffff' ? '#ff4544' : cl;
    var color_rgb = this.hexToRgb(cl_b);
    var back_g = 'rgba(' + color_rgb.r + ',' + color_rgb.g + ',' + color_rgb.b + ',.2)'
    this.setData({
      all_color: cl ? cl : '#ffffff',
      back_g: back_g
      // back_end: back_end
    });
    wx.setNavigationBarColor({
      frontColor: cl != '#ffffff' ? '#ffffff' : '#000000',
      backgroundColor: cl,
      animation: {
        duration: 0,
        timingFunc: 'easeIn'
      }
    })
  },
  hexToRgb: function (hex) {
    var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
    return result ? {
      r: parseInt(result[1], 16),
      g: parseInt(result[2], 16),
      b: parseInt(result[3], 16)
    } : null;
  },
  bindContentInput: function(t) {
    // var store_config = this.data.store_config;
    // store_config.content = t.detail.value, this.setData({
    //   store_config: store_config
    // });
    this.data.mch_list[t.currentTarget.dataset.index].content = t.detail.value, this.setData({
      mch_list: this.data.mch_list
    });
  },
  KeyName: function(t) {
    var store_config = this.data.store_config;
    store_config.offline_name = t.detail.value, this.setData({
      store_config: store_config
    });
  },
  KeyMobile: function(t) {
    var store_config = this.data.store_config;
    store_config.offline_mobile = t.detail.value, this.setData({
      store_config: store_config
    });
  },
  getOffline: function(t) {
    var e = this,
      a = t.currentTarget.dataset.offline,
      p = t.currentTarget.dataset.peisong,
      i = t.currentTarget.dataset.index,
      store_config = e.data.store_config,
      s = e.data.mch_list;
    store_config.offline = a, store_config.is_peisong = p, e.setData({
      store_config: store_config
    })
    e.getPrice();
  },
  getPeisong: function(t) {
    var e = this,
      a = t.currentTarget.dataset.peisong,
      i = t.currentTarget.dataset.index,
      s = e.data.mch_list;
    s[i].is_peisong = a, s[i].offline = 1, e.setData({
      mch_list: s
    }), 1 == s.length && 0 == s[0].mch_id && 1 == s[0].is_peisong ? e.setData({
      is_peisong: 1
    }) : e.setData({
      is_peisong: 0
    }), e.getPrice();
  },
  location: function () {
    var t = this;
    wx.openLocation({
      latitude: parseFloat(t.data.store_config.shop.latitude),
      longitude: parseFloat(t.data.store_config.shop.longitude),
      address: t.data.store_config.shop.address,
      name: t.data.store_config.shop.name
    });
  },

  dingwei: function() {
    var t = this;
    wx.chooseLocation({
      success: function(e) {
        a = e.longitude, i = e.latitude, t.setData({
          location: e.address
        }), t.getOrderData(t.data.options);
      },
      fail: function(a) {
        e.getauth({
          content: "需要获取您的地理位置授权，请到小程序设置中打开授权",
          success: function(e) {
            e && (e.authSetting["scope.userLocation"] ? t.dingwei() : wx.showToast({
              title: "您取消了授权",
              image: "/images/icon-warning.png"
            }));
          }
        });
      }
    });
  },  
  orderSubmitMessge: function (t) {
    var that = this;
    wx.getSetting({
      withSubscriptions: true,
      complete(res) {
        console.log(res, 'resres')
      }
    })
    if (typeof wx.requestSubscribeMessage === "function"){
      var tmplIdsInfo = that.data.tmplIdsInfo ? that.data.tmplIdsInfo : []
      wx.requestSubscribeMessage({
        tmplIds: that.data.tmplIdsInfo ? that.data.tmplIdsInfo : [],
        success: function (res) {
          // wx.showModal({
          //   title: '提示',
          //   content: JSON.stringify(res) + JSON.stringify(tmplIdsInfo),
          //   success() {

          //   }
          // })
          if (tmplIdsInfo.length>=1 && res[tmplIdsInfo[0]] == "accept") {
            that.data.form_id.push(tmplIdsInfo[0])
          }
          if (tmplIdsInfo.length >= 2 && res[tmplIdsInfo[1]] == "accept") {
            that.data.form_id.push(tmplIdsInfo[1])
          }
          if (tmplIdsInfo.length >= 3 && res[tmplIdsInfo[2]] == "accept") {
            that.data.form_id.push(tmplIdsInfo[2])
          }
        },
        fail: function (res) {
          console.log(res);
          // wx.showModal({
          //   title: '提示',
          //   content: JSON.stringify(res) + JSON.stringify(tmplIdsInfo),
          //   success() {
          //   }
          // })
        },
        complete: function (res) {
          that.orderSubmit(t)
        }
      })
    }else {
      that.orderSubmit(t)
    }
  },
  orderSubmit: function(t) {
    this.showedit('')
    if (this.data.payment < 0) return this.showPayment();
    var store_config = this.data.store_config;
    if (!store_config.shop && store_config.offline == 1 ) {
      return wx.showModal({
        title: "提示",
        content: "请选择社区",
        showCancel: !1
      })
    }
    if (store_config.offline == 1 
      && store_config.is_peisong != 1 
      && !store_config.offline_mobile 
      && !store_config.offline_name) return wx.showModal({
      title: "提示",
      content: "请填写收货人信息",
      showCancel: !1
    }), !1;
    if (store_config.is_peisong == 1 && !this.data.address.latitude) return wx.showModal({
      title: "提示",
      content: "请选择地址经纬度",
      showCancel: !1
    }), !1;
    if (store_config.is_peisong == 1 
      && store_config.is_peisong_type == 1 
      && store_config.peisong_juli > 0 
      && !store_config.distributor_setting
      && this.data.distance > store_config.peisong_juli) {
      var that = this;
      return wx.showModal({
        title: '提示',
        content: '当前社区离您较远,暂不支持送货上门,请重新选择收货地址',
        showCancel: false,
        success (res) {
          
        }
      })
    }
    var e = this, a = {}, i = e.data.mch_list;
    for (var s in i) {
      var n = i[s].form;
      if (store_config.is_peisong == 1 || store_config.offline == 0){
        if (!e.data.address) return wx.showModal({
          title: "提示",
          content: "请选择收货地址",
          showCancel: !1
        }), !1;
        a.address_id = e.data.address.id;
      }
      if (n && 1 == n.is_form) {
        var o = n.list;
        for (var r in o) if (1 == o[r].required) if ("radio" == o[r].type || "checkbox" == o[r].type) {
          var d = !1;
          for (var c in o[r].default_list) 1 == o[r].default_list[c].is_selected && (d = !0);
          if (!d) return wx.showModal({
            title: "提示",
            content: "请填写" + n.name + "，加‘*’为必填项",
            showCancel: !1
          }), !1;
        } else if (!o[r].default || void 0 == o[r].default) return wx.showModal({
          title: "提示",
          content: "请填写" + n.name + "，加‘*’为必填项",
          showCancel: !1
        }), !1;
      }
    }
    a.store_config = JSON.stringify(store_config) ;
    if (a.mch_list = JSON.stringify(i), e.data.pond_id > 0) {
      if (e.data.express_price > 0 && -1 == e.data.payment) return e.setData({
        show_payment: !0
      }), !1;
    } else if (-1 == e.data.payment) return e.setData({
      show_payment: !0
    }), !1;
    1 == e.data.integral_radio ? a.use_integral = 1 : a.use_integral = 2, a.payment = e.data.payment,
      a.formIds = JSON.stringify(e.data.form_id), a.formId = e.data.form_id[0], e.order_submit(a, "s");
  },
  onReady: function() {},
  onShow: function(t) {
    if (!n) {
      n = !0, e.pageOnShow(this);
      var a = this,
        i = wx.getStorageSync("picker_address");
      i && a.setData({
        address: i
      }), a.getOrderData(a.data.options);
    }
    var user_info = wx.getStorageSync("user_info");
    if (!(user_info && user_info.id > 0)) {
      return false;
    }
    var a = this;
    var s = wx.getStorageSync("picker_order");
    var store_config = a.data.store_config;
    if (s && store_config ) {
      store_config.shop = s;
      if (s.peisong_type) {
        store_config.peisong_type = s.peisong_type;
      }
      var mch_list = a.data.mch_list;
      for (var ii =0 ;ii < mch_list.length; ii++) {
        mch_list[ii].peisong_price = s.peisong_price;
      }
      store_config.peisong_price = s.peisong_price ? s.peisong_price : 0;
      var distance = s.distance;
       a.setData({
         store_config: store_config,
         distance: distance,
         mch_list: mch_list
      })
      a.getPrice();
      wx.removeStorageSync('picker_order')
    }
  },
  getOrderData: function(s) {
    var user_info = wx.getStorageSync("user_info");
    if (!(user_info && user_info.id > 0)) {
      return false;
    }
    var n = this,
      o = {},
      r = "";
    n.data.address && n.data.address.id && (r = n.data.address.id), o.address_id = r,
      o.longitude = a, o.latitude = i, wx.showLoading({
        title: "正在加载",
        mask: !0
      }), o.mch_list = s.mch_list, e.request({
        url: t.order.new_submit_preview,
        method: "POST",
        data: o,
        success: function(t) {
          if (wx.hideLoading(), 0 == t.code) {
            if (t.data.store_config.shop&&t.data.store_config.shop.shop_time) {
              n.setPeisongTime(t.data.store_config.shop.shop_time);
            }
            var e = wx.getStorageSync("input_data"),
              a = t.data,
              i = -1,
              s = 1,
              o = a.mch_list,
              r = [];
            if (n.data.store_config) {
              n.data.store_config.peisong_price = a.store_config.peisong_price
              a.store_config = n.data.store_config
            }
            e && (r = e.mch_list, i = e.payment, s = e.integral_radio), a.integral_radio = s;
            for (var d in a.pay_type_list) {
              if (i == a.pay_type_list[d].payment) {
                a.payment = i;
                break;
              }
              if (1 == a.pay_type_list.length) {
                a.payment = a.pay_type_list[d].payment;
                break;
              }
            }
            for (var d in o) {
              var c = {},
                l = {};
              // if (n.data.store_config) {
              //   o[d].peisong_price = n.data.store_config.peisong_price;
              // }
              if (a.store_config) {
                o[d].peisong_price = a.store_config.peisong_price;
              }
              if (o[d].show = !1, o[d].show_length = o[d].goods_list.length - 1, 0 != r.length)
                for (var _ in r) o[d].mch_id == r[_].mch_id && (o[d].content = r[_].content,
                  o[d].form = r[_].form, c = r[_].shop, l = r[_].picker_coupon, o[d].offline_name = r[_].offline_name,
                  o[d].offline_mobile = r[_].offline_mobile);
              if (l)
                for (var _ in o[d].coupon_list)
                  if (l.id == o[d].coupon_list[_].id) {
                    o[d].picker_coupon = l;
                    break;
                  }
              o[d].send_type && 2 == o[d].send_type ? (o[d].offline = 1, n.setData({
                mch_offline: !1
              })) : o[d].offline = 0;
            }
            a.mch_list = o;
            var h = n.data.index; 
            n.setData(a), n.getPrice();
          }
          1 == t.code && wx.showModal({
            title: "提示",
            content: t.msg,
            showCancel: !1,
            confirmText: "返回",
            success: function(t) {
              t.confirm && wx.navigateBack({
                delta: 1
              });
            }
          });
        },
        complete: function(t) {
          if (n.data.mch_list && n.data.mch_list[0].send_type == 0) {
            var a = 1,
              i = 0,
              s = n.data.mch_list;
            s[i].offline = a, s[i].is_peisong = n.data.is_peisong, n.setData({
              mch_list: s
            }), 1 == s.length && 0 == s[0].mch_id && 1 == s[0].offline ? n.setData({
              mch_offline: !1
            }) : n.setData({
              mch_offline: !0,
              is_peisong: 0
            }), n.getPrice();
          }
          
          if (n.data.store_config && n.data.store_config.shop) {
            if (n.data.address) {
              var dis = n.distance(n.data.address.latitude, n.data.address.longitude, n.data.store_config.shop.latitude, n.data.store_config.shop.longitude)
              n.setData({
                distance: dis
              })
            }else {
              wx.getLocation({
                success: function (res) {
                  var dis = n.distance(res.latitude, res.longitude, n.data.store_config.shop.latitude, n.data.store_config.shop.longitude)
                  n.setData({
                    distance: dis
                  })
                }
              });
            }
            
          }
        }
      });
  },
  updateAddress: function () {
    var n = this;
    if (n.data.store_config && n.data.store_config.shop) {
      wx.chooseLocation({
        success: function (res) {
          var dis = n.distance(res.latitude, res.longitude, n.data.store_config.shop.latitude, n.data.store_config.shop.longitude)
          n.setData({
            distance: dis
          })
        }
      });
    }
  },
  setPeisongTime: function (time) {
    var arr = time.split("-");
    var peisong_time= [
      { index: 0, time: '任何时间' }
    ];
    if (arr.length > 1) {
      arr[0] = parseInt(arr[0]);
      arr[1] = parseInt(arr[1]);
    }else {
      return false;
    }
    if (arr[0] < arr[1]) {
      for (var i=arr[0] ;i<arr[1];i++) {
        var str = i + ':00~' + (i+1) + ':00'
        peisong_time.push({ index: i, time: str})
      }
      this.setData({
        peisong_time: peisong_time
      })
    } else {
      return false;
    }
  },
  defaultShop: function() {
    var that = this;
    wx.getStorage({
      key: 'address_info',
      success: function(res) {
        return true;
      },
      fail: function() { return false;
        
      }
    })
  },
  showCouponPicker: function(t) {
    var e = this,
      a = t.currentTarget.dataset.index,
      i = e.data.mch_list;
    e.getInputData(), i[a].coupon_list && i[a].coupon_list.length > 0 && e.setData({
      show_coupon_picker: !0,
      coupon_list: i[a].coupon_list,
      index: a
    });
  },
  pickCoupon: function(t) {
    var e = this,
      a = t.currentTarget.dataset.index,
      i = e.data.index,
      s = wx.getStorageSync("input_data");
    wx.removeStorageSync("input_data");
    var n = s.mch_list;
    "-1" == a || -1 == a ? (n[i].picker_coupon = !1, s.show_coupon_picker = !1) : (n[i].picker_coupon = e.data.coupon_list[a],
      s.show_coupon_picker = !1), s.mch_list = n, s.index = -1, e.setData(s), e.getPrice();
  },

  pickCouponSubmit: function (t) {
    var e = this,
      a = e.data.pick_coupon_index,
      i = e.data.index,
      s = wx.getStorageSync("input_data");
    wx.removeStorageSync("input_data");
    var n = s.mch_list;
    "-1" == a || -1 == a ? (n[i].picker_coupon = !1, s.show_coupon_picker = !1) : (n[i].picker_coupon = e.data.coupon_list[a],
      s.show_coupon_picker = !1), s.mch_list = n, s.index = -1, e.setData(s), e.getPrice();
  },
  pickCouponIndex:function (t){
    var e = this,
      a = t.currentTarget.dataset.index;
      e.setData({
        pick_coupon_index:a
      })
  },
  pickCouponHiden:function (){
      this.setData({
        show_coupon_picker:!1
      })
  },

  showShop: function(t) {
    var e = this,
      a = t.currentTarget.dataset.index;
    e.getInputData(), e.setData({
      index: a
    }), e.dingwei();
  },
  pickShop: function(t) {
    var e = this,
      a = t.currentTarget.dataset.index,
      i = e.data.index,
      s = wx.getStorageSync("input_data"),
      n = s.mch_list;
    "-1" == a || -1 == a ? (n[i].shop = !1, s.show_shop = !1) : (n[i].shop = e.data.shop_list[a],
      s.show_shop = !1), s.mch_list = n, s.index = -1, e.setData(s), e.getPrice();
  },
  integralSwitchChange: function(t) {
    var e = this;
    0 != t.detail.value ? e.setData({
      integral_radio: 1
    }) : e.setData({
      integral_radio: 2
    }), e.getPrice();
  },
  integration: function(t) {
    var e = this.data.integral.integration;
    wx.showModal({
      title: "积分使用规则",
      content: e,
      showCancel: !1,
      confirmText: "我知道了",
      confirmColor: "#ff4544",
      success: function(t) {
        t.confirm;
      }
    });
  },
  getPrice: function() {
    var t = this,
      e = t.data.mch_list,
      ech = t.data.mch_list,
      a = t.data.integral_radio,
      p = t.data.store_config,
      i = (t.data.integral,
        0),
      s = 0,
      n = {};
    for (var o in e) {
      var r = e[o],
      d = (parseFloat(r.total_price), parseFloat(r.level_price));
      var level_total = (parseFloat(r.total_price) - parseFloat(r.level_price)).toFixed(2);
      r.picker_coupon && r.picker_coupon.sub_price > 0 && (d -= r.picker_coupon.sub_price),
        r.integral && r.integral.forehead > 0 && 1 == a && (d -= parseFloat(r.integral.forehead)),
        0 == p.offline && (r.express_price && (d += r.express_price), r.offer_rule && 1 == r.offer_rule.is_allowed && (n = r.offer_rule),
          1 == r.is_area && (s = 1)), i += parseFloat(d);
      if (p.is_peisong == 1) {
        i += parseFloat(r.peisong_price ? r.peisong_price : 0);
      }
    }
    
    i = i >= 0 ? i : 0, t.setData({
      new_total_price: parseFloat(i.toFixed(2)),
      offer_rule: n,
      is_area: s,
      level_total:level_total
    });
  },
  cardDel: function() {
    this.setData({
      show_card: !1
    }), wx.redirectTo({
      url: "/pages/order/order?status=1"
    });
  },
  cardTo: function() {
    this.setData({
      show_card: !1
    }), wx.redirectTo({
      url: "/pages/card/card"
    });
  },
  formInput: function(t) {
    var e = this,
      a = t.currentTarget.dataset.index,
      i = t.currentTarget.dataset.formId,
      s = e.data.mch_list,
      n = s[a].form,
      o = n.list;
    o[i].default = t.detail.value, n.list = o, e.setData({
      mch_list: s
    });
  },
  selectForm: function(t) {
    var e = this,
      a = e.data.mch_list,
      i = t.currentTarget.dataset.index,
      s = t.currentTarget.dataset.formId,
      n = t.currentTarget.dataset.k,
      o = a[i].form,
      r = o.list,
      d = r[s].default_list;
    if ("radio" == r[s].type) {
      for (var c in d) c == n ? d[n].is_selected = 1 : d[c].is_selected = 0;
      r[s].default_list = d;
    }
    "checkbox" == r[s].type && (1 == d[n].is_selected ? d[n].is_selected = 0 : d[n].is_selected = 1,
      r[s].default_list = d), o.list = r, a[i].form = o, e.setData({
      mch_list: a
    });
  },
  showPayment: function(t) {
    this.setData({
      show_payment: !0
    });
  },
  payPicker: function(t) {
    var e = t.currentTarget.dataset.index;
    //this.showedit()
    this.setData({
      payment: e,
      show_payment: !1
    });
  },
  payClose: function() {
    this.setData({
      show_payment: !1
    });
  },
  getInputData: function() {
    var t = this,
      e = t.data.mch_list,
      a = {
        integral_radio: t.data.integral_radio,
        payment: t.data.payment,
        mch_list: e
      };
    wx.setStorageSync("input_data", a);
  },
  onHide: function() {
    e.pageOnHide(this), this.getInputData();
  },
  onUnload: function() {
    e.pageOnUnload(this), wx.removeStorageSync("input_data");
  },
  uploadImg: function(t) {
    var a = this,
      i = t.currentTarget.dataset.index,
      s = t.currentTarget.dataset.formId,
      o = a.data.mch_list,
      r = o[i].form;
    n = !0, e.uploader.upload({
      start: function() {
        wx.showLoading({
          title: "正在上传",
          mask: !0
        });
      },
      success: function(t) {
        0 == t.code ? (r.list[s].default = t.data.url, a.setData({
          mch_list: o
        })) : a.showToast({
          title: t.msg
        });
      },
      error: function(t) {
        a.showToast({
          title: t
        });
      },
      complete: function() {
        wx.hideLoading();
      }
    });
  },
  goToAddress: function() {
    n = !1, wx.navigateTo({
      url: "/pages/address-picker/address-picker"
    });
  },
  showMore: function(t) {
    var e = this,
      a = e.data.mch_list,
      i = t.currentTarget.dataset.index;
    a[i].show = !a[i].show, e.setData({
      mch_list: a
    });
  },
  showedit: function(tt) {
    var e = this;
    var i = e.data.mch_list;
    var store_config = e.data.store_config;

    store_config.total_price = 0;
    store_config.express_price = 0;
   
    if (store_config.is_peisong == 1 
      && store_config.is_peisong_type == 1 
      && store_config.peisong_juli > 0 
      && !store_config.distributor_setting
      && this.data.distance.replace("km","") > store_config.peisong_juli) {
        var that = this;
        return wx.showModal({
          title: '提示',
          content: '当前社区离您较远,暂不支持送货上门,请重新选择收货地址',
          showCancel: false,
          success (res) {
            
          }
        }), !1;
    }
    for (var s in i) {
      store_config.total_price += +i[s].total_price;
      store_config.express_price += +i[s].express_price;
      if (this.data.is_area == 1 && store_config.offline == 0) {
        return wx.showModal({
          title: "提示",
          content: "该区域暂时缺货",
          showCancel: !1
        })
      }
      if (!e.data.address && store_config.offline==0) return wx.showModal({
        title: "提示",
        content: "请选择收货地址",
        showCancel: !1
      }), !1;

      if (!e.data.address && store_config.offline == 1 && store_config.is_peisong ==1) return wx.showModal({
          title: "提示",
          content: "请选择收货地址",
          showCancel: !1
      }), !1;

      if (store_config.offline == 1 && store_config.is_peisong != 1 && (!store_config.offline_name || !store_config.offline_mobile)) return wx.showModal({
        title: "提示",
        content: "请填写收货人信息",
        showCancel: !1
      }), !1;

      if (e.data.payment < 0) return this.showPayment(), !1;
    }
    store_config.total_price = store_config.total_price.toFixed(2);
    store_config.express_price = store_config.express_price.toFixed(2);
    var that = this;
    that.setData({
      store_config: store_config
    })
    if (that.data.editTrue) {
      that.setData({
        editTrue: !1
      })

    } else {
      that.setData({
        editTrue: true
      })
    }

    var tmplIdsInfo = [];
    for (var key in that.data.message_info) {
      if (key == "send_tpl" || key == 'shop_send_tpl') {
        // if (that.data.store_config.offline == 1 && key == 'shop_send_tpl') {
        //   tmplIdsInfo.push(that.data.message_info[key])
        // }
        if (key == 'send_tpl') {
          tmplIdsInfo.push(that.data.message_info[key])
        }
      }else {
        tmplIdsInfo.push(that.data.message_info[key])
      }
    }
    setTimeout(() => {
      that.setData({
        tmplIdsInfo: tmplIdsInfo
      })
    }, 300);


  },
  distance: function (la1, lo1, la2, lo2) {

    var La1 = la1 * Math.PI / 180.0;

    var La2 = la2 * Math.PI / 180.0;

    var La3 = La1 - La2;

    var Lb3 = lo1 * Math.PI / 180.0 - lo2 * Math.PI / 180.0;

    var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(La3 / 2), 2) + Math.cos(La1) * Math.cos(La2) * Math.pow(Math.sin(Lb3 / 2), 2)));

    s = s * 6378.137; //地球半径

    s = Math.round(s * 10000) / 10000;
    return s.toFixed(2) + 'km';
  },
  go_cat_list:function (){
    wx.switchTab({
      url: '/pages/cat/cat',
    })
  },
  peisong_time() {
    var that = this;
    this.setData({
      show_time:that.data.show_time == 1 ? 0 : 1,
      change_peisong_time: '任何时间'
    })
  },
  PsTimeChange: function (ps) {
    var val = ps.detail.value;
    val = this.data.peisong_time[val].time
    this.setData({
      change_peisong_time: val
    })
  },
  PsTimeConfirm:function(){
    var store_config = this.data.store_config;
    store_config.peisong_time = this.data.change_peisong_time;
    this.setData({
      store_config: store_config,
      show_time: 0
    })
  }

});